package cloud.maque.admin.impl;

import cloud.maque.admin.domain.entity.MqVarGroup;
import cloud.maque.admin.mapper.MqVarGroupMapper;
import cloud.maque.admin.service.MqVarGroupService;
import cloud.maque.admin.service.MqVarService;
import cloud.maque.framework.common.request.PaginateDataResquest;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author 4color
 * @description mq_var_group
 * @date 2023-03-17
 */
@Service
public class MqVarGroupServiceImpl extends ServiceImpl<MqVarGroupMapper, MqVarGroup> implements MqVarGroupService {


    @Autowired
    MqVarService varService;

    @Override
    public MqVarGroup saveGroup(MqVarGroup mqVar) {
        Assert.hasText(mqVar.getVarGroupName(), "组名称不能为空");
        saveOrUpdate(mqVar);
        return mqVar;
    }

    @Override
    public Boolean delete(String groupId, String tenantId) {

        if (varService.countByGroupId(groupId) > 0) {
            throw new RuntimeException("组下面还有具体参数");
        }

        QueryWrapper<MqVarGroup> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MqVarGroup::getTenantId, tenantId);
        wrapper.lambda().eq(MqVarGroup::getVarGroupId, groupId);
        return remove(wrapper);
    }

    @Override
    public PageInfo<MqVarGroup> page(PaginateDataResquest<MqVarGroup> pageReq) {
        PageHelper.startPage(pageReq.getPageIndex(), pageReq.getPageSize());
        QueryWrapper<MqVarGroup> wrapper = new QueryWrapper<>();

        if (!StrUtil.isEmptyIfStr(pageReq.getData().getVarGroupName())) {
            wrapper.lambda().like(MqVarGroup::getVarGroupName, pageReq.getData().getVarGroupName());
        }

        if (!StrUtil.isEmptyIfStr(pageReq.getData().getTenantId())) {
            wrapper.lambda().eq(MqVarGroup::getTenantId, pageReq.getData().getTenantId());
        }

        wrapper.lambda().orderByAsc(MqVarGroup::getVarGroupOrder);
        wrapper.lambda().orderByAsc(MqVarGroup::getVarGroupId);
        List<MqVarGroup> lists = this.baseMapper.selectList(wrapper);
        PageInfo<MqVarGroup> pageInfo = new PageInfo<>(lists);

        return pageInfo;
    }
}
